package com.turtle3d.gui.drawingpanel;

/*                         \\|//
 {o o}
 ~~~~~~~~~~~~~~~~~~~~~~~oOOo~\-/~oOOo~~~~~~~~~~~~~~~~~~~~~~~~
 |               DESGRAFF enr, DECOY PROJECT                |
 |                 Copyright David Deschenes                    |
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Nom:  CProject.java
 Date: may 25 2003, update: 26 May 2003
 Auteur: David Deschenes
 Desc: Project file using CFrameWork for Java3D projects
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/


import javax.media.j3d.Appearance;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.GeometryArray;
import javax.media.j3d.IndexedTriangleArray;
import javax.media.j3d.Material;
import javax.media.j3d.Shape3D;
import javax.media.j3d.TriangleArray;
import javax.vecmath.Color3f;
import javax.vecmath.Point3f;


import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.Box;
import com.turtle3d.visualizator.Line3D;

public class CProject extends CFrameWork {

	/**
	 * Fonction : createSceneGraph () params: void retour: void desc: creating
	 * the SceneGraph (content)
	 */
	public BranchGroup createSceneGraph() {
		// ********Build Content branch
		oBranchGroup_1 = new BranchGroup();

		// Transform3D oTransform3D_1 = new Transform3D();
		// TransformGroup oTransformGroup_1 = null;

		// ********Assemble branch
		oBranchGroup_1.addChild(grid());		
		drawShape( (new Line3D(new Point3f(0,0,0), new Point3f(0,2,0), new Color3f(1,1,1)).toShape3D()));		
				
		addKeyMouseSupport();
		oBranchGroup_1.compile();
		return oBranchGroup_1;
	}
	
	public void drawShape(Shape3D shape){
		oBranchGroup_1.addChild(shape);
	}

	/**
	 * Fonction : addBox() params: float float float retour: Box(Shape3D) desc:
	 * creating the SceneGraph (content)
	 */
	public Box addBox(float x, float y, float z) {

		Appearance oAppearance_1 = new Appearance();
		Material oMaterial_1 = new Material();
		oMaterial_1.setDiffuseColor(new Color3f(1, 1, 1));
		oMaterial_1.setSpecularColor(new Color3f(1, 1, 1));
		oMaterial_1.setShininess(10.0f);
		oAppearance_1.setMaterial(oMaterial_1);
		Box box = new Box(x, y, z, oAppearance_1);
		return box;
	}

	/*
	 * public Shape3D triangle(Point3f p){ IndexedTriangleArray triangle = new
	 * IndexedTriangleArray(3, GeometryArray.COORDINATES |
	 * GeometryArray.COLOR_3, 12);
	 * 
	 * oIndexedTriangleArray_1.setCoordinate(0, new Point3f( 0.0f, 2.0f, 0.0f
	 * )); //5 vertex oIndexedTriangleArray_1.setCoordinate(1, new Point3f(
	 * -1.5f, 0.0f, -1.5f )); oIndexedTriangleArray_1.setCoordinate(2, new
	 * Point3f( -1.5f, 0.0f, 1.5f ));
	 *  }
	 */

	public Shape3D triangle(Point3f p1, Point3f p2, Point3f p3) {
		TriangleArray triangleArray1 = new TriangleArray(3,
				GeometryArray.COORDINATES | GeometryArray.COLOR_3);

		triangleArray1.setCoordinate(0, p1);
		triangleArray1.setCoordinate(1, p2);
		triangleArray1.setCoordinate(2, p3);

		Color3f oColor3f_1 = new Color3f(1.0f, 0.0f, 0.0f);
		triangleArray1.setColor(0, oColor3f_1);

		Shape3D oShape3D_1 = new Shape3D(triangleArray1);
		return oShape3D_1; // return the pyramid

	}

	/**
	 * Fonction : pymamid() params: void retour: Shape3D desc: creating the
	 * SceneGraph (content)
	 */
	// public
	public Shape3D pymamid() {

		// Constructs an empty IndexedTriangleArray object with the specified
		// number of vertices, vertex format, and number of indices.

		IndexedTriangleArray oIndexedTriangleArray_1 = new IndexedTriangleArray(
				5, GeometryArray.COORDINATES | GeometryArray.COLOR_3, 12);

		oIndexedTriangleArray_1.setCoordinate(0, new Point3f(0.0f, 2.0f, 0.0f)); // 5
																					// vertex
		oIndexedTriangleArray_1.setCoordinate(1,
				new Point3f(-1.5f, 0.0f, -1.5f));
		oIndexedTriangleArray_1
				.setCoordinate(2, new Point3f(-1.5f, 0.0f, 1.5f));
		oIndexedTriangleArray_1.setCoordinate(3, new Point3f(1.5f, 0.0f, 1.5f));
		oIndexedTriangleArray_1
				.setCoordinate(4, new Point3f(1.5f, 0.0f, -1.5f));

		// Draw triangle between 3 indexed vertexes...
		oIndexedTriangleArray_1.setCoordinateIndex(0, 0);
		oIndexedTriangleArray_1.setCoordinateIndex(1, 1);
		oIndexedTriangleArray_1.setCoordinateIndex(2, 2);

		oIndexedTriangleArray_1.setCoordinateIndex(3, 0);
		oIndexedTriangleArray_1.setCoordinateIndex(4, 2);
		oIndexedTriangleArray_1.setCoordinateIndex(5, 3);

		oIndexedTriangleArray_1.setCoordinateIndex(6, 0);
		oIndexedTriangleArray_1.setCoordinateIndex(7, 3);
		oIndexedTriangleArray_1.setCoordinateIndex(8, 4);

		oIndexedTriangleArray_1.setCoordinateIndex(9, 0);
		oIndexedTriangleArray_1.setCoordinateIndex(10, 4);
		oIndexedTriangleArray_1.setCoordinateIndex(11, 1);

		// this section of code proves that Java3D indexed Triangle shapes is
		// buggy
		// Sets the color associated with the vertex at the specified index for
		// this object
		Color3f oColor3f_1 = new Color3f(1.0f, 1.0f, 0.0f);
		oIndexedTriangleArray_1.setColor(0, oColor3f_1);

		Shape3D oShape3D_1 = new Shape3D(oIndexedTriangleArray_1);
		return oShape3D_1; // return the pyramid
	}

	public static void main(String argv[]) {
		/*
		 * CProject applet = new CProject(); JFrame frame = new JFrame("Cam");
		 * frame.setDefaultCloseOperation(frame.DISPOSE_ON_CLOSE);
		 * frame.getContentPane().add(applet); frame.setSize(640,480);
		 * applet.init(); applet.start(); frame.setVisible(true);
		 */

		CProject test = new CProject();
		
		
		new MainFrame(test, 800, 600);

	}
}
